package com.warm.pump.module.skd.controller;

import com.alibaba.fastjson.JSON;
import com.warm.pump.frame.gen.bean.PageBean;
import com.warm.pump.frame.gen.util.MyBeanUtils;
import com.warm.pump.frame.gen.util.OutJson;
import com.warm.pump.frame.util.RRException;
import com.warm.pump.frame.util.StringUtils;
import com.warm.pump.module.skd.bean.InJobJson;
import com.warm.pump.module.skd.bean.po.gen.ScheduleJob;
import com.warm.pump.module.skd.service.IScheduleJobService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.CronExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * skd_job
 */
@Controller
@RequestMapping("/scheduleJob")
public class ScheduleJobController {
    @Autowired
    private IScheduleJobService scheduleJobService;

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("scheduleJob:list")
    public OutJson list(Integer page, Integer limit, ScheduleJob scheduleJob) {
        Map<String, Object> map = new HashMap<>();
        MyBeanUtils.copyBean2Map(map, scheduleJob);

        if (StringUtils.isNotEmpty(scheduleJob.getSidx()) && StringUtils.isNotEmpty(scheduleJob.getOrder())) {
            map.put("orderBy", scheduleJob.getSidx() + " " + scheduleJob.getOrder());
        }
        //查询列表数据
        PageBean pageBean = scheduleJobService.getPageBeanByMap(map, page, limit);

        return OutJson.ok().put("page", pageBean);
    }


    /**
     * 信息
     */
    @ResponseBody
    @RequestMapping("/info/{id}")
    @RequiresPermissions("scheduleJob:info")
    public OutJson info(@PathVariable("id") Long id) {
        ScheduleJob scheduleJob = scheduleJobService.queryObject(id);

        return OutJson.ok().put("scheduleJob", scheduleJob);
    }


    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping("/save")
    @RequiresPermissions("scheduleJob:save")
    public OutJson save(@RequestBody ScheduleJob scheduleJob) {
        scheduleJob.setCreateTime(new Date());
        scheduleJob.setUpdateTime(new Date());
        scheduleJobService.save(scheduleJob);

        return OutJson.ok();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("scheduleJob:update")
    public OutJson update(@RequestBody ScheduleJob scheduleJob) {
        scheduleJob.setUpdateTime(new Date());
        scheduleJobService.update(scheduleJob);

        return OutJson.ok();
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("scheduleJob:delete")
    public OutJson delete(@RequestBody Long[] ids) {
        scheduleJobService.deleteBatch(ids);

        return OutJson.ok();
    }

    @RequestMapping({"/job/checkCron"})
    @ResponseBody
    public OutJson checkCron(String cron) {
        try {
            CronExpression.validateExpression(cron);
        } catch (ParseException e) {
            throw new RRException("cron表达式有误," + e.getMessage());
        }
        return OutJson.ok();
    }


    @RequestMapping("/runFromOut")
    @ResponseBody
    public OutJson runFromOut(@RequestBody InJobJson inJobJson) {
        if (null == inJobJson.getId()) {
            return OutJson.error("id不能为空");
        }
        String params = null;
        if (null != inJobJson.getCond() && inJobJson.getCond().size() > 0) {
            params = JSON.toJSONString(inJobJson.getCond());
        }
        scheduleJobService.run(inJobJson.getId(), params);
        return OutJson.ok();
    }

    /**
     * 立即执行任务
     */
    @ResponseBody
    @RequestMapping("/run")
    public OutJson run(@RequestBody Long[] jobIds) {
        scheduleJobService.run(jobIds);
        return OutJson.ok();
    }

    /**
     * 暂停定时任务
     */
    @ResponseBody
    @RequestMapping("/pause")
    public OutJson pause(@RequestBody Long[] jobIds) {
        scheduleJobService.pause(jobIds);
        return OutJson.ok();
    }

    /**
     * 恢复定时任务
     */
    @ResponseBody
    @RequestMapping("/resume")
    public OutJson resume(@RequestBody Long[] jobIds) {
        scheduleJobService.resume(jobIds);
        return OutJson.ok();
    }
}
